import {
    selectAll,
    allId,
    add,
    update,
    del
} from '@/api/section'

const getDefaultState = () => {
    return {}
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    }
}

const actions = {
    // use select all
    selectAll({
        commit
    }, sectionInfo) {
        return new Promise((resolve, reject) => {
            const {
                obj,
                page,
                pageSize
            } = sectionInfo
            selectAll({
                obj: obj,
                page: page,
                pageSize: pageSize
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    allId({
        commit
    }, sectionInfo) {
        return new Promise((resolve, reject) => {
            const {
                name,
                hospitalId
            } = sectionInfo
            allId({
                name: name,
                hospitalId: hospitalId
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use add menu
    add({
        commit
    }, sectionInfo) {
        return new Promise((resolve, reject) => {
            const {
                sectionId,
                hospitalId,
                name
            } = sectionInfo
            add({
                sectionId: sectionId,
                hospitalId: hospitalId,
                name: name
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use update
    update({
        commit
    }, sectionInfo) {
        return new Promise((resolve, reject) => {
            const {
                sectionId,
                hospitalId,
                name
            } = sectionInfo
            update({
                sectionId: sectionId,
                hospitalId: hospitalId,
                name: name
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use delete 
    delete({
        commit
    }, ids) {
        return new Promise((resolve, reject) => {
            del(ids).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}